﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Drawing;
using System.Windows.Forms;

namespace WinNodeEditorDemo.Blender
{
    /// <summary>
    /// 此类仅演示 作为MixRGB节点的下拉选择框弹出菜单
    /// </summary>
    public class FrmEnumSelect : Form
    {
        private Point m_pt;         // 弹出菜单显示的位置
        private int m_nWidth;       // 弹出菜单的宽度
        private float m_scale;      // 缩放比例
        private List<object> m_lst = new List<object>();  // 存储枚举值的列表
        private StringFormat m_sf;  // 字符串格式化对象，用于绘制文本

        public Enum Enum { get; set; }  // 属性，用于存储用户选择的枚举值

        private bool m_bClosed;  // 标志窗口是否已关闭

        // 构造函数，接受枚举类型、位置、宽度和缩放比例作为参数
        public FrmEnumSelect(Enum e, Point pt, int nWidth, float scale)
        {
            // 设置窗体样式，双缓冲等
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

            // 获取枚举类型的所有值，并添加到列表中
            foreach (var v in Enum.GetValues(e.GetType()))
                m_lst.Add(v);

            // 初始化窗体属性和参数
            this.Enum = e;
            m_pt = pt;
            m_scale = scale;
            m_nWidth = nWidth;

            // 初始化字符串格式化对象
            m_sf = new StringFormat();
            m_sf.LineAlignment = StringAlignment.Center;

            // 设置窗体样式
            this.ShowInTaskbar = false;
            this.BackColor = Color.FromArgb(255, 34, 34, 34);  // 设置背景色
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;  // 设置无边框
        }
        /// <summary>
        /// OnLoad 方法：重载窗体加载事件，设置窗体的位置、大小。
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.Location = m_pt;
            this.Width = (int)(m_nWidth * m_scale);
            this.Height = (int)(m_lst.Count * 20 * m_scale);
        }
        /// <summary>
        /// OnPaint 方法：重载绘制事件，绘制窗体上的文本
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            g.ScaleTransform(m_scale, m_scale);
            Rectangle rect = new Rectangle(0, 0, this.Width, 20);
            foreach (var v in m_lst)
            {
                g.DrawString(v.ToString(), this.Font, Brushes.White, rect, m_sf);
                rect.Y += rect.Height;
            }
        }
        /// <summary>
        /// OnMouseClick 方法：重载鼠标点击事件，处理用户点击选择的枚举值。
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseClick(MouseEventArgs e)
        {
            base.OnMouseClick(e);
            int nIndex = e.Y / (int)(20 * m_scale);
            if (nIndex >= 0 && nIndex < m_lst.Count) this.Enum = (Enum)m_lst[nIndex];
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            m_bClosed = true;
        }
        /// <summary>
        /// OnMouseLeave 方法：重载鼠标离开事件，处理窗体关闭。
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            if (m_bClosed) return;
            //this.DialogResult = System.Windows.Forms.DialogResult.None;
            this.Close();
        }
    }
}
